package com.yjf.ljr.activity;

import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.yjf.ljr.R;
import com.yjf.ljr.common.CommonData;
import com.yjf.ljr.connection.IjConnect;
import com.yjf.ljr.dataclass.ForgetPasswordDataClass;
import com.yjf.ljr.parser.RequestBuilder;
import com.yjf.ljr.parser.RequestBuilder.RequestObject;

public class ForgetPasswordActivity extends IjActivity implements OnClickListener {

	@ID("etFindPwd")
	private EditText mEtFindPwd;
	@ID("tv_title_btn_left")
	private TextView mTvTitleBtnLeft;
	@ID("btFinPwd")
	private Button mBtFindPwd;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_forget_password);
		initControl();
	}

	private void initControl() {
		setLeftBtnClick();
		setTitleStr("忘记密码");
		mBtFindPwd.setOnClickListener(this);

	}

	@Override
	public void onClick(View view) {
		
		switch (view.getId()) {
		case R.id.btFinPwd:
			if (TextUtils.isEmpty(mEtFindPwd.getText().toString())) {
				showToast("请输入要找回的用户名！");
			} else {
				showProgressDialog();
				new ObtainPhoneTask(mEtFindPwd.getText().toString()).execute();
			}
			break;		
		default:
			break;
		}
	}

	class ObtainPhoneTask extends AsyncTask<Void, Void, String> {

		private ForgetPasswordDataClass dc = new ForgetPasswordDataClass();
		private String userName;
		private String mFailStr = "";

		public ObtainPhoneTask(String userName) {
			super();
			this.userName = userName;
		}

		@Override
		protected void onPreExecute() {
			super.onPreExecute();
		}

		@Override
		protected String doInBackground(Void... arg0) {
			RequestObject requestObject = new RequestObject();
			requestObject.method = "forgetLoginPwd";
			requestObject.map.put("userName", userName);
			String requestbuildstr = RequestBuilder.build(requestObject);
			try {
				IjConnect.doRequest(requestbuildstr, dc);
				if (dc.code == "") {
					mFailStr = getResources().getString(R.string.loaddata_exception);
				} else if (!(dc.code.equals(CommonData.RESULT_SUCCESS))) {
					mFailStr = dc.msg;
				}
			} catch (Exception e) {
				mFailStr = getResources().getString(R.string.loaddata_exception);
				e.printStackTrace();
			}
			return null;
		}

		@Override
		protected void onPostExecute(String result) {
			super.onPostExecute(result);
			if (!(TextUtils.isEmpty(mFailStr))) {
				dismissProgressDialog();
				showToast(mFailStr);
			} else {
				dismissProgressDialog();
				Intent intent = new Intent(mContext, MobileVerificationActivity.class);
				intent.putExtra("obtain_mobile", dc.mobile);
				startActivity(intent);
				finish();
			}
		}
	}

}
